<?php
class GuildService {
    private static $instance;

    public static function get() {
        if (!self::$instance) {
            self::$instance = new GuildService();
        }
        return self::$instance;
    }

    protected function __construct(){

    }

    /**
     * 获取公会信息
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function getData($userId) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = $this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1000
            );
        }
        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $positionNames = $redisDb->hGetAll(GameConst::GuildPositionName.$guildId);
        $positionNames = $positionNames ? $positionNames : array();

        $donateInfo = $this->getUserDonateInfo($profileInfoList);

        $guildInfo->setNum(count($guildMemberInfoList->getList()));
        SzPersister::get()->setVo($guildInfo);

        $data = array(
            'guild' => array(
                'id' => $guildInfo->getId(),
                'name' => $guildInfo->getName(),
                'lv' => $guildInfo->getLv(),
                'slogan' => $guildInfo->getSlogan(),
                'notice' => $guildInfo->getNotice(),
                'money' => $guildInfo->getMoney(),
                'donateTimes' => $donateInfo['donateTimes'],
                'donateTime' => $donateInfo['donateTime'],
                'positionNames' => $positionNames,
                'updateTime' => $guildInfo->getUpdateTime(),
                'addShareNum' => $guildInfo->getAddShareNum()
            ),
            'code' => GameConst::CODE_1000
        );

        return $data;
    }

    /**
     * 获取公会成员信息
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function members($userId) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = $this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $players = array();

        if ($guildMemberInfoList->toArray()) {
            foreach ($guildMemberInfoList->toArray() as $value) {
                $players[] = $this->getMemberInfo($value['userId'],$value['position'],$value['money']);
            }
        }

        return array(
            'players' => $players,
            'code' => GameConst::CODE_1000
        );
    }

    /**
     * 解散公会
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function dissolve($userId) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $userMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);

        if(count($guildMemberInfoList->toArray()) > 1) {
            return array(
                'code' => GameConst::CODE_1011 // 踢除所有成员才能解散公会
            );
        }

        $userMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$userMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        if ($userMemberInfo->getPosition() > 1) {
            return array(
                'code' => GameConst::CODE_1007, // 无操作权限
            );
        }

        /* @var GuildInfoModel $model
         * @var GuildInfoVo $vo
         */
        $model = SzPersister::get()->getModel(GameConst::ORM_NAME_GUILD_INFO);
        $vo = $model->retrieve(null, $guildId);
        $model->deleteVoCache($vo);
        $model->delete($vo);

        $guildMemberInfoList->deleteElement($userId,false);
        SzPersister::get()->setVoList($guildMemberInfoList);

        $this->setUserGuildId($userId,0,$profileInfoList);

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $data = $redisDb->hGetAll(GameConst::GuildApplyList.$guildId);
        if ($data) {
            foreach ($data as $targetId=>$applyTime) {
                $redisDb->hDel(GameConst::UserGuildApplyList.$targetId,$guildId);
            }
        }
        $redisDb->delete(GameConst::GuildApplyList.$guildId);

        $appConfig = SzConfig::get()->loadAppConfig('app');
        $redis = new Redis();
        if ($appConfig['ENV'] == 'DEV') {
            $redisConfig = array(
                'server' => '127.0.0.1',
                'port' => 6379,
                'password' => ''
            );
        } else {
            $redisConfig = array(
                'server' => '10.10.2.73',
                'port' => 6379,
                'password' => 'QOlrZqF7r0ELytSZ'
            );
        }

        $redis->connect($redisConfig['server'],$redisConfig['port']);
        if ($redisConfig['password']) {
            $redis->auth($redisConfig['password']);
        }
        $redis->delete('GuildChat:'.$guildId);

        //社团竞技期间进行数据删除，丢失本季赛事公用数据以及工会任务
        UnionCompetitionService::get()->deleteCompetitionData($userId,$guildId,GameService::get()->getReqTime());

        return array(
            'code' => GameConst::CODE_1000
        );
    }

    /**
     * 获取公会列表
     * @param $userId
     * @param $start
     * @param $end
     * @return array
     * @throws SzException
     */
    public function getList($userId,$start,$end) {
        $serversConfigs = SzConfig::get()->loadAppConfig('database', 'MySql');
        $instance = new SzMySqlDb($serversConfigs[0]);
        $guildList = $instance->select("SELECT `id`,`name`,`slogan`,`num`,`lv`  FROM `ft_guild_info` order by lv desc,`num` desc, `lv` asc, `id` asc limit {$start},{$end}");

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);

        $applyList = $redisDb->hGetAll(GameConst::UserGuildApplyList.$userId);

        return array(
            'code' => GameConst::CODE_1000,
            'guildList' => $guildList,
            'applyList' => $applyList
        );
    }

    /**
     * 创建公会
     * @param $userId
     * @param $name
     * @param $reqTime
     * @return array                
     * @throws SzException
     */
    public function create($userId,$name,$reqTime) {
        $name = GameService::get()->delEmjoy($name);
        if (!$name) {
            return array(
                'code' => GameConst::CODE_1022,
            );
        }
        $serversConfigs = SzConfig::get()->loadAppConfig('database', 'MySql');
        $instance = new SzMySqlDb($serversConfigs[0]);
        $sql = "SELECT `id`,`name`,`slogan`,`num`,`lv`  FROM `ft_guild_info` where `name` = '{$name}'";
        $res = $instance->select($sql);
        if ($res) {
            return array(
                'code' => GameConst::CODE_1016,
            );
        }

        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = $this->getUserGuildId($userId,$profileInfoList);
        if ($guildId) {
            return array(
                'code' => GameConst::CODE_1001,
                'guildId' => $guildId
            );
        }

        $persekutuanBuat = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'persekutuanBuat', true,false);
        if (!$persekutuanBuat) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }
        $persekutuanBuat = json_decode($persekutuanBuat['value'],true);
        $input = '';

        foreach ($persekutuanBuat as $entityId => $count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true);
            $input .= $entityId.':'.$count.'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD,$userId,0,$input,'create');//消耗资源上报

        $guildId = null;
        $guildInfo = new GuildInfoVo($userId, $guildId, $userId, $name, 1, '', '', 0, $reqTime, 1, 0,0,true);
        /**
         * @var SzMySqlModel $model
         */
        $model = SzPersister::get()->getModel(GameConst::ORM_NAME_GUILD_INFO);
        list(, $insertId) = $model->insert($guildInfo);
        if ($insertId) {
            $guildId = $insertId;
            $guildInfo->setId($guildId);
            $guildInfo->removeInsertFlag();
            $model->setVoCache($guildInfo);
            SzPersister::get()->setVo($guildInfo);
        }

        // 设置成员信息
        $this->setGuildMemberInfo($userId,$guildId,1);

        // 设置用户公会id
        $this->setUserGuildId($userId,$guildId,$profileInfoList);

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:club|action:create|',
                'item' => $guildId,
                'value' => 1
            )
        ));

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => $input,
                'item' => 'club_create&'.$guildId,
                'value' => 1
            )
        ));

        TrainingService::get()->updateProgress($userId,GameConst::TRAINING_TASK_JOIN_GUILD,1);

        return array(
            'code' => GameConst::CODE_1000,
            'guildId' => $guildId,
            'name' => $name
        );
    }

    /**
     * 查询公会信息
     * @param $type
     * @param $keyword
     * @return array
     * @throws SzException
     */
    public function search($type,$keyword) {
        $keyword = GameService::get()->delEmjoy($keyword);
        if (!$keyword) {
            return array(
                'code' => GameConst::CODE_1000,
                'guildInfo' => array()
            );
        }
        $serversConfigs = SzConfig::get()->loadAppConfig('database', 'MySql');
        $instance = new SzMySqlDb($serversConfigs[0]);
        $sql = 'SELECT `id`,`name`,`slogan`,`num`,`lv`  FROM `ft_guild_info` where ';
        if ($type == 0) {
            $sql .= ' id = '.$keyword;
        } else {
            $sql .= " name like '%{$keyword}%'";
        }
        return array(
            'code' => GameConst::CODE_1000,
            'guildInfo' => $instance->select($sql)
        );
    }

    /**
     * 查看申请者列表
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function applicant($userId) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $applyList = $redisDb->hGetAll(GameConst::GuildApplyList.$guildId);

        $data = array();

        if ($applyList) {
            foreach ($applyList as $userId=>$applyTime) {
                $data[] = $this->getMemberInfo($userId,0,0);
            }
        }

        return array(
            'code' => GameConst::CODE_1000,
            'applicant' => $data
        );

    }

    /**
     * 申请公会
     * @param $userId
     * @param $guildId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function join($userId,$guildId,$reqTime) {
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        if ($this->getUserGuildId($userId,$profileInfoList)) {
            return array(
                'code' => GameConst::CODE_1001 // 已加入公会
            );
        }
        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_1002 // 公会不存在
            );
        }

       $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);

        // 获取用户申请列表
        $userGuildApplyList = $redisDb->hGetAll(GameConst::UserGuildApplyList.$userId);
        if (array_key_exists($guildId,$userGuildApplyList)) {
            return array(
                'code' => GameConst::CODE_1003 // 公会已申请
            );
        }

        // 获取公会申请列表
        $guildApplyList = $redisDb->hGetAll(GameConst::GuildApplyList.$guildId);
        if (array_key_exists($guildId,$guildApplyList)) {
            $redisDb->hSet(GameConst::UserGuildApplyList.$userId,$guildId,$reqTime);
            return array(
                'code' => GameConst::CODE_1003 // 公会已申请
            );
        }

        if (count($guildApplyList) > 100) {
            return array(
                'code' => GameConst::CODE_1004 // 该公会申请已达上限
            );
        }

        $level = $guildInfo->getLv();
        $guildLevelSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_LEVEL, $level, true,false);
        if (!$guildLevelSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        if ($guildLevelSetting['jumlahOrang'] <= $guildInfo->getNum()) {
            return array(
                'code' => GameConst::CODE_1006,
            );
        }

        // 申请公会
        $redisDb->hSet(GameConst::UserGuildApplyList.$userId,$guildId,$reqTime);
        $redisDb->hSet(GameConst::GuildApplyList.$guildId,$userId,$reqTime);

        return array(
            'code' => GameConst::CODE_1000,
        );
    }

    /**
     * 修改公会名称
     * @param $userId
     * @param $name
     * @return array
     * @throws SzException
     */
    public function changeName($userId, $name) {
        $name = GameService::get()->delEmjoy($name);
        if (!$name) {
            return array(
                'code' => GameConst::CODE_1022,
            );
        }
        $serversConfigs = SzConfig::get()->loadAppConfig('database', 'MySql');
        $instance = new SzMySqlDb($serversConfigs[0]);
        $sql = "SELECT `id`,`name`,`slogan`,`num`,`lv`  FROM `ft_guild_info` where `name` = '{$name}'";
        $res = $instance->select($sql);
        if ($res) {
            return array(
                'code' => GameConst::CODE_1016,
            );
        }
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        if ($guildMemberInfo->getPosition() > 1) {
            return array(
                'code' => GameConst::CODE_1007, // 无权限操作
            );
        }

        $persekutuanNama = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'persekutuanNama', true,false);
        if (!$persekutuanNama) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }
        $persekutuanNama = json_decode($persekutuanNama['value'],true);
        $output = '';

        foreach ($persekutuanNama as $entityId => $count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true);
            $output .= $entityId.':'.$count.'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD,$userId,0,$output,'changeName');//消耗资源上报

        $guildInfo->setName($name);
        SzPersister::get()->setVo($guildInfo);

        return array(
            'code' => GameConst::CODE_1000,
            'name' => $name
        );
    }

    /**
     * 修改公会公告或标语
     * @param $userId
     * @param $name
     * @param $type 1:标语 2:公告
     * @return array
     * @throws SzException
     */
    public function changeTxt($userId,$type,$name) {
        $name = GameService::get()->delEmjoy($name);
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        $position = $guildMemberInfo->getPosition();
        $guildPermissionSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_PERMISSION, $position, true,false);
        if (!$guildPermissionSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        if (!$guildPermissionSetting['announcement']) {
            return array(
                'code' => GameConst::CODE_1007, // 无权限操作
            );
        }

        if ($type == 1) {
            // 修改标语
            $guildInfo->setSlogan($name);
        } else {
            // 修改公告
            $guildInfo->setNotice($name);
        }
        SzPersister::get()->setVo($guildInfo);

        return array(
            'code' => GameConst::CODE_1000,
            'content' => $name
        );
    }

    /**
     * 同意加入公会
     * @param $userId
     * @param $targetId
     * @return array
     * @throws SzException
     */
    public function accept($userId,$targetId) {
        return $this->operation($userId,$targetId,1);
    }

    /**
     * 拒绝加入公会
     * @param $userId
     * @param $targetId
     * @return array
     * @throws SzException
     */
    public function refuse($userId,$targetId) {
        return $this->operation($userId,$targetId,0);
    }

    /**
     * 拒绝加入工会
     * @param $userId
     * @return bool
     * @throws SzException
     */
    public function batchDelUsers($userId) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         * @var $targetProfileInfoList ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return false;
        }
        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return false;
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            return false;
        }

        $position = $guildMemberInfo->getPosition();
        $guildPermissionSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_PERMISSION, null, true,false);
        if (!isset($guildPermissionSetting[$position])) {
            return false;
        }

        $guildPermissionSetting = $guildPermissionSetting[$position];

        if (!$guildPermissionSetting['application']) {
            return false;
        }

        $applyList = $redisDb->hGetAll(GameConst::GuildApplyList.$guildId);
        if (empty($applyList)){
            return false;
        }

        foreach ($applyList as $key=>$data){
            $redisDb->hDel(GameConst::GuildApplyList.$guildId,$key);
            $redisDb->hDel(GameConst::UserGuildApplyList.$key,$guildId);
        }

        return true;
    }

    /**
     * 操作申请列表
     * @param $userId
     * @param $targetId
     * @param $type 0:拒绝  1:同意
     * @return array
     * @throws SzException
     */
    public function operation($userId,$targetId,$type) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         * @var $targetProfileInfoList ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);

        if (!$redisDb->hGet(GameConst::GuildApplyList.$guildId,$targetId)) {
            $redisDb->hDel(GameConst::UserGuildApplyList.$targetId,$guildId);
            return array(
                'code' => GameConst::CODE_1012, // 对方未申请公会
            );
        }

        $targetProfileInfoList = SzPersister::get()->getVoList($targetId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        if ($this->getUserGuildId($targetId,$targetProfileInfoList)) {
            $redisDb->hDel(GameConst::GuildApplyList.$guildId,$targetId);
            $redisDb->hDel(GameConst::UserGuildApplyList.$targetId,$guildId);
            return array(
                'code' => GameConst::CODE_1001, // 对方已加入公会
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        $position = $guildMemberInfo->getPosition();
        $guildPermissionSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_PERMISSION, null, true,false);
        $len = count($guildPermissionSetting);
        if (!isset($guildPermissionSetting[$position])) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        $guildPermissionSetting = $guildPermissionSetting[$position];

        if (!$guildPermissionSetting['application']) {
            return array(
                'code' => GameConst::CODE_1007, // 无权限操作
            );
        }
        $memberInfo = array();
        if ($type) {
            $level = $guildInfo->getLv();
            $guildLevelSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_LEVEL, $level, true,false);
            if (!$guildLevelSetting) {
                return array(
                    'code' => GameConst::CODE_9998,
                );
            }

            if ($guildLevelSetting['jumlahOrang'] <= $guildInfo->getNum()) {
                return array(
                    'code' => GameConst::CODE_1006,
                );
            }

            $targetMemberInfo = new GuildMemberInfoVo($guildId, $targetId, $len, 0, 0, 0, true);

            $guildMemberInfoList->addElement($targetMemberInfo);
            $guildMemberInfoList->setElement($targetId,$targetMemberInfo);
            SzPersister::get()->setVoList($guildMemberInfoList);

            $this->setUserGuildId($targetId,$guildId,$targetProfileInfoList);
            $memberInfo = $this->getMemberInfo($targetId,$len,0);

            $guildInfo->setNum(count($guildMemberInfoList->getList()));
            SzPersister::get()->setVo($guildInfo);

            StatisticsService::get()->saveLog($targetId, GameConst::LOG_TYPE_USER_ORBIT, array(
                'clientData' => array(
                    'tid' => 'Module:club|action:join|',
                    'item' => $guildId,
                    'value' => 1
                )
            ));

            TrainingService::get()->updateProgress($targetId,GameConst::TRAINING_TASK_JOIN_GUILD,1);
        }

        $redisDb->hDel(GameConst::GuildApplyList.$guildId,$targetId);
        $redisDb->hDel(GameConst::UserGuildApplyList.$targetId,$guildId);

        return array(
            'code' => GameConst::CODE_1000,
            'memberInfo' => $memberInfo
        );
    }

    /**
     * 退出公会
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function quit($userId) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        if ($guildMemberInfo->getPosition() == 1) {
            return array(
                'code' => GameConst::CODE_1008, // 无权限操作
            );
        }

        $guildMemberInfoList->deleteElement($userId,false);
        SzPersister::get()->setVoList($guildMemberInfoList);

        $guildInfo->setNum(count($guildMemberInfoList->getList()));
        SzPersister::get()->setVo($guildInfo);

        $this->setUserGuildId($userId,0,$profileInfoList);

        //社团赛数据更新
        UnionCompetitionService::get()->quitGuildUpdateData($userId,$guildId,$guildInfo,GameService::get()->getReqTime());

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:club|action:del|',
                'item' => $guildId,
                'value' => 1
            )
        ));

        return array(
            'code' => GameConst::CODE_1000
        );
    }

    /**
     * 降职/升职
     * @param $userId
     * @param $targetId
     * @param $position
     * @return array
     * @throws SzException
     */
    public function promotion($userId,$targetId,$position) {
        $guildPermissionSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_PERMISSION, $position, true,false);
        if (!$guildPermissionSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        $level = $guildInfo->getLv();
        $guildLevelSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_LEVEL, $level, true,false);
        if (!$guildLevelSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $userMemberInfo GuildMemberInfoVo
         * @var $targetMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $userMemberInfo = $guildMemberInfoList->getElement($userId,false);
        $targetMemberInfo = $guildMemberInfoList->getElement($targetId,false);
        if (!$userMemberInfo || !$targetMemberInfo) {
            return array(
                'code' => GameConst::CODE_1002,
            );
        }

        if ($targetMemberInfo->getPosition() <= $userMemberInfo->getPosition()) {
            return array(
                'code' => GameConst::CODE_1007, // 无操作权限
            );
        }

        if ($position < 4) {
            $maxNum = 0;
            foreach ($guildMemberInfoList->toArray() as $guildMember) {
                if ($guildMember['position'] == $position) {
                    $maxNum++;
                }
            }
            $configNum = 0;
            if ($position == 2) {
                $configNum = $guildLevelSetting['vicePresident'];
            } elseif ($position == 3) {
                $configNum = $guildLevelSetting['experience'];
            }

            if ($maxNum >= $configNum) {
                return array(
                    'code' => GameConst::CODE_1010, // 公会职位已达上限
                );
            }
        }

        $targetMemberInfo->setPosition($position);
        $guildMemberInfoList->updateElement($targetId,$targetMemberInfo);
        SzPersister::get()->setVoList($guildMemberInfoList);

        return array(
            'code' => GameConst::CODE_1000,
        );
    }

    /**
     * 转让会长
     * @param $userId
     * @param $targetId
     * @return array
     * @throws SzException
     */
    public function transferLeader($userId,$targetId) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        $guildPermissionSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_PERMISSION, null, true,false);
        if (!$guildPermissionSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $userMemberInfo GuildMemberInfoVo
         * @var $targetMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $userMemberInfo = $guildMemberInfoList->getElement($userId,false);
        $targetMemberInfo = $guildMemberInfoList->getElement($targetId,false);
        if (!$userMemberInfo || !$targetMemberInfo) {
            return array(
                'code' => GameConst::CODE_1005,
            );
        }

        if ($userMemberInfo->getPosition() > 1) {
            return array(
                'code' => GameConst::CODE_1007, // 无操作权限
            );
        }

        $guildInfo->setUserId($targetId);
        $guildInfo->setLeaderId($targetId);
        SzPersister::get()->setVo($guildInfo);

        $targetMemberInfo->setPosition(1);
        $guildMemberInfoList->updateElement($targetId,$targetMemberInfo);

        $userMemberInfo->setPosition(count($guildPermissionSetting));
        $guildMemberInfoList->updateElement($userId,$userMemberInfo);

        SzPersister::get()->setVoList($guildMemberInfoList);
        return array(
            'code' => GameConst::CODE_1000,
        );
    }

    /**
     * 公会升级
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function upgrade($userId) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        $level = $guildInfo->getLv();
        $guildLevelSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_LEVEL, $level, true,false);
        if (!$guildLevelSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        if (!$guildLevelSetting['peraga']) {
            return array(
                'code' => GameConst::CODE_1014,
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $userMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $userMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$userMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        if ($userMemberInfo->getPosition() > 1) {
            return array(
                'code' => GameConst::CODE_1007, // 无操作权限
            );
        }

        $money = $guildInfo->getMoney();
        if ($money < $guildLevelSetting['peraga']) {
            return array(
                'code' => GameConst::CODE_1013,
            );
        }

        $guildInfo->updateMoney(-$guildLevelSetting['peraga']);
        $guildInfo->updateLv(1);

        SzPersister::get()->setVo($guildInfo);

        $nowLevel = $level + 1;

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:club|action:upgrade|',
                'item' => 'club_upgrade&'.$nowLevel,
                'value' => 1
            )
        ));

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_UPGRADE, array(
            'clientData' => array(
                'tid' => 'Module:club|action:upgrade|',
                'item' => "fromlevel:{$level}|changelev:1|targetlev:{$nowLevel}|",
                'value' => 1
            )
        ));

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => "740110000001:1|{$guildLevelSetting['peraga']}",
                'item' => "club_upgrade",
                'value' => 1
            )
        ));

        //记录
        $reqTime = time();
        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $message = array(
            'userId' => $userId,
            'type' => GameConst::GUILD_LOG_TYPE2,
            'sid' => -1,
            'num' => $guildLevelSetting['peraga'],
            'pos' => $userMemberInfo->getPosition(),
            'time' => $reqTime,
            'lv' => $nowLevel,
            'share' => -1,
        );

        $newKey = $reqTime;
        $hasData = $redisDb->hGet(GameConst::CACHE_KEY_GUILD_SKILL_USE_LIST.$guildId,$reqTime);
        if ($hasData){
            $newKey = $reqTime + 1;
        }
        $redisDb->hSet(GameConst::CACHE_KEY_GUILD_SKILL_USE_LIST.$guildId,$newKey,json_encode($message));

        return array(
            'code' => GameConst::CODE_1000,
        );
    }

    /**
     * 修改职位名称
     * @param $userId
     * @param $position
     * @param $name
     * @return array
     * @throws SzException
     */
    public function changePositionName($userId,$position,$name) {
        $guildPermissionSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_PERMISSION, null, true,false);
        if (!$guildPermissionSetting || $position > count($guildPermissionSetting) || $position < 1 || !$name) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $userMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $userMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$userMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        if ($userMemberInfo->getPosition() > 1) {
            return array(
                'code' => GameConst::CODE_1007, // 无操作权限
            );
        }

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $redisDb->hSet(GameConst::GuildPositionName.$guildId,$position,$name);

        return array(
            'code' => GameConst::CODE_1000,
        );
    }

    /**
     * 踢除公会
     * @param $userId
     * @param $targetId
     * @return array
     * @throws SzException
     */
    public function ban($userId,$targetId) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         * @var $targetGuildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        $targetGuildMemberInfo = $guildMemberInfoList->getElement($targetId,false);
        if (!$targetGuildMemberInfo) {
            return array(
                'code' => GameConst::CODE_1000,
            );
        }

        $position = $guildMemberInfo->getPosition();
        $guildPermissionSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_PERMISSION, $position, true,false);
        if (!$guildPermissionSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        if (!$guildPermissionSetting['kickingPeople']) {
            return array(
                'code' => GameConst::CODE_1007, // 无权限操作
            );
        }

        /**
         * @var $targetProfileInfoList ModuleProfileInfoVoList
         */
        $targetProfileInfoList = SzPersister::get()->getVoList($targetId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $targetGuildId =$this->getUserGuildId($targetId,$targetProfileInfoList);

        if (!$targetGuildId || $targetGuildId != $guildId) {
            return array(
                'code' => GameConst::CODE_1005,
            );
        }

        $donateInfo = $this->getUserDonateInfo($targetProfileInfoList);
        $donateTime = $donateInfo['donateTime'];
        if ((date('Ymd',$donateTime) == date('Ymd',GameService::get()->getReqTime())) && $targetGuildMemberInfo->getMoney()) {
            return array(
                'code' => GameConst::CODE_1023
            );
        }

        if ($targetGuildId && $targetGuildId == $guildId) {
            $this->setUserGuildId($targetId,0,$targetProfileInfoList);
        }

        $guildMemberInfoList->deleteElement($targetId,false);
        SzPersister::get()->setVoList($guildMemberInfoList);

        $guildInfo->setNum(count($guildMemberInfoList->getList()));
        SzPersister::get()->setVo($guildInfo);

        //社团赛数据更新
        UnionCompetitionService::get()->quitGuildUpdateData($targetId,$guildId,$guildInfo,GameService::get()->getReqTime());

        StatisticsService::get()->saveLog($targetId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:club|action:del|',
                'item' => $guildId,
                'value' => 1
            )
        ));

        return array(
            'code' => GameConst::CODE_1000,
        );
    }

    /**
     * 捐献
     * @param $userId
     * @param $id
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function donate($userId,$id,$reqTime) {
        $guildDonateSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_DONATE, $id, true,false);
        if (!$guildDonateSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        $persekutuanjumlahDonasi = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'persekutuanjumlahDonasi', true,false);
        if (!$persekutuanjumlahDonasi) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        $persekutuanjumlahDonasi = $persekutuanjumlahDonasi ['value'];

        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }
        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        $donateInfo = $this->getUserDonateInfo($profileInfoList);
        $donateTimes = $donateInfo['donateTimes'];
        $donateTime = $donateInfo['donateTime'];

        $updateTime = null;
        if (date('Ymd',$donateTime) != date('Ymd',$reqTime)) {
            $donateTimes = 0;
            $updateTime = $reqTime;
        }
        if ($donateTimes >= $persekutuanjumlahDonasi) {
            return array(
                'code' => GameConst::CODE_1015,
            );
        }

        $donateTimes++;
        $input = $output = '';
        foreach ($guildDonateSetting['konsumsi'] as $entryId=>$count) {
            GameService::get()->handleEntity($userId, $entryId, $count, true, true);
            $input .= $entryId.':'.$count.'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD,$userId,0,$input,'donate');//消耗资源上报

        foreach ($guildDonateSetting['dapatkan'] as $entryId=>$count) {
            GameService::get()->handleEntity($userId, $entryId, $count, true, true);
            $output .= $entryId.':'.$count.'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD,$userId,1,$output,'donate');//获得资源上报

        $this->setUserDonateInfo($userId,$profileInfoList,$donateTimes,$updateTime);
        $guildMemberInfo->updateMoney($guildDonateSetting['peraga']);
        $guildMemberInfoList->updateElement($userId,$guildMemberInfo);
        SzPersister::get()->setVoList($guildMemberInfoList);

        $guildInfo->updateMoney($guildDonateSetting['peraga']);
        SzPersister::get()->setVo($guildInfo);

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:club|action:donation|',
                'item' => 'club_donation&'.$id,
                'value' => 1
            )
        ));

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => $input,
                'item' => 'club_donation&'.$guildId,
                'value' => 1
            )
        ));

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_OUTPUT, array(
            'clientData' => array(
                'tid' => $output,
                'item' => 'club_donation'.$guildId,
                'value' => 1
            )
        ));

        //公会拥有的资金总数上报
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:club|action:moneyTotal|',
                'item' => 'club_moneyTotal&'.$guildId.'&'.$guildInfo->getMoney(),
                'value' => 1
            )
        ));

        /**
         * @var ModuleItemVoList $itemList
         * @var ModuleItemVo $item
         */
        $itemList = SzPersister::get()->getVoList($userId, ModuleItemConst::ORM_NAME_ITEM);

        $coin = 0;
        foreach ($itemList->getList() as $itemDefId => $items) {
            foreach ($items as $item) {
                if ($itemDefId == 19000005){
                    $coin += $item->getCount();
                }
            }
        }

        //自己拥有的公会币上报
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:member|action:coin|',
                'item' => 'member_coin&'.$coin,
                'value' => 1
            )
        ));

        //自己的公会贡献上报
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:member|action:donation|',
                'item' => 'member_donation&'.$guildMemberInfo->getMoney(),
                'value' => 1
            )
        ));


        //当日的捐献资金上报
        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => $input,
                'item' => 'club_donation&day&'.$guildId,
                'value' => 1
            )
        ));

        // 更新合约任务进度
        $contractData = ContractService::get()->getContractOpenAndTask($userId,GameConst::CONTRACT_TASK_7,$reqTime);
        if (!empty($contractData) && !empty($contractData['task']) && !empty($contractData['session'])){
            if ($reqTime >= $contractData['start'] && $reqTime <= $contractData['end']){
                ContractService::get()->updateTaskProgress($userId,$contractData['session'],$contractData['task']['pos'],$contractData['task']['taskId'],$reqTime);
            }
        }

        return array(
            'code' => GameConst::CODE_1000,
            'donateTimes' => $donateTimes,
            'donateTime' => $donateTime
        );
    }

    /**
     * 获取花盆信息
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function flowerpotInfo($userId) {
        /**
         * @var $guildFlowerpotInfoList GuildFlowerpotInfoVoList
         */
        $guildFlowerpotInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_FLOWERPOT_INFO);
        return array(
            'code' => GameConst::CODE_1000,
            'guildFlowerpotList' => $guildFlowerpotInfoList->toArray()
        );
    }

    /**
     * 解锁花盆
     * @param $userId
     * @param $position
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function unlock($userId,$position,$reqTime) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildFlowerpotInfoList GuildFlowerpotInfoVoList
         * @var $guildFlowerpotInfo GuildFlowerpotInfoVo
         */
        $guildFlowerpotInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_FLOWERPOT_INFO);
        $guildFlowerpotInfo = $guildFlowerpotInfoList->getElement($position,false);
        if ($guildFlowerpotInfo) {
            return array(
                'code' => GameConst::CODE_1017, // 公会花盆已解锁
            );
        }

        $level = $guildInfo->getLv();
        $guildLevelSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_LEVEL, $level, true,false);
        if (!$guildLevelSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        if ($position > $guildLevelSetting['potBunga']) {
            return array(
                'code' => GameConst::CODE_1018, // 公会等级未到达解锁等级
            );
        }

        $persekutuanFlower = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'persekutuanFlower', true,false);
        if (!$persekutuanFlower) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }
        $persekutuanFlower = json_decode($persekutuanFlower['value'],true);

        $input = '';
        foreach ($persekutuanFlower as $entryId=>$count) {
            GameService::get()->handleEntity($userId, $entryId, $count, true, true);
            $input .= $entryId.':'.$count.'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD,$userId,0,$input,'unlock');//消耗资源上报

        $guildFlowerpotInfo = new GuildFlowerpotInfoVo($userId,$position,1,0,0,$reqTime,$guildId,true);
        $guildFlowerpotInfoList->addElement($guildFlowerpotInfo);
        $guildFlowerpotInfoList->setElement($position,$guildFlowerpotInfo);
        SzPersister::get()->setVoList($guildFlowerpotInfoList);

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:clubPot|action:unlock|',
                'item' => $position,
                'value' => 1
            )
        ));

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => $input,
                'item' => 'clubPot_unclock&'.$position,
                'value' => 1
            )
        ));

        return array(
            'code' => GameConst::CODE_1000
        );
    }

    /**
     * 升级花盆
     * @param $userId
     * @param $position
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function upgradeFlowerpot($userId,$position,$reqTime) {
        /**
         * @var $guildFlowerpotInfoList GuildFlowerpotInfoVoList
         * @var $guildFlowerpotInfo GuildFlowerpotInfoVo
         */
        $guildFlowerpotInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_FLOWERPOT_INFO);
        $guildFlowerpotInfo = $guildFlowerpotInfoList->getElement($position,false);
        if (!$guildFlowerpotInfo) {
            return array(
                'code' => GameConst::CODE_1019, // 公会花盆未解锁
            );
        }
        $flowerId = $guildFlowerpotInfo->getFlowerId();
        $level = $guildFlowerpotInfo->getLevel();
        $updateTime = $guildFlowerpotInfo->getUpdateTime();
        $guildFlowerpotSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_FLOWERPOT, $level, true,false);
        if (!$guildFlowerpotSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }
        if (!$guildFlowerpotSetting['upgrade']) {
            return array(
                'code' => GameConst::CODE_1021, // 花盆到达最大等级
            );
        }

        if ($flowerId) {
            $guildFlowerHarvestSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_FLOWER_HARVEST, $flowerId, true,false);
            if (!$guildFlowerHarvestSetting) {
                return array(
                    'code' => GameConst::CODE_9998,
                );
            }

            $harvestTime = intval($guildFlowerHarvestSetting['time'] * (100 - $guildFlowerpotSetting['shorten']) / 100);

            if (($reqTime - $updateTime) >= $harvestTime) {
                return array(
                    'code' => GameConst::CODE_1020, // 请先收获花
                );
            }
        }

        $input = '';

        foreach ($guildFlowerpotSetting['upgrade'] as $entryId=>$count) {
            GameService::get()->handleEntity($userId, $entryId, $count, true, true);
            $input .= $entryId.':'.$count.'|';
        }
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD,$userId,0,$input,'upgradeFlowerpot');//消耗资源上报

        $guildFlowerpotInfo->updateLevel(1);
        $guildFlowerpotInfoList->updateElement($position,$guildFlowerpotInfo);
        SzPersister::get()->setVoList($guildFlowerpotInfoList);

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
            'clientData' => array(
                'tid' => 'Module:clubPot|action:upgrade|',
                'item' => 'clubPot_upgrade&'.$position,
                'value' => 1
            )
        ));
        $nowLevel = $level + 1;

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_UPGRADE, array(
            'clientData' => array(
                'tid' => 'objective:clubPot|item:'.$position.'|',
                'item' => "fromlevel:{$level}|changelev:1|targetlev:{$nowLevel}|",
                'value' => 1
            )
        ));

        StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_INPUT, array(
            'clientData' => array(
                'tid' => $input,
                'item' => 'clubPot_upgrade&'.$position,
                'value' => 1
            )
        ));

        return array(
            'code' => GameConst::CODE_1000
        );
    }

    /**
     * 公会花盆收获
     * @param $userId
     * @param $positions
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function harvest($userId,$positions,$reqTime) {
        /**
         * @var $guildFlowerpotInfoList GuildFlowerpotInfoVoList
         * @var $guildFlowerpotInfo GuildFlowerpotInfoVo
         */
        $guildFlowerpotInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_FLOWERPOT_INFO);
        $flowerList = array();
        $output = '';
        foreach ($positions as $position) {
            $guildFlowerpotInfo = $guildFlowerpotInfoList->getElement($position,false);
            if (!$guildFlowerpotInfo) {
                continue;
            }
            $flowerId = $guildFlowerpotInfo->getFlowerId();
            $level = $guildFlowerpotInfo->getLevel();
            $updateTime = $guildFlowerpotInfo->getUpdateTime();
            $guildFlowerpotSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_FLOWERPOT, $level, true,false);
            if (!$guildFlowerpotSetting) {
                return array(
                    'code' => GameConst::CODE_9998,
                );
            }
            $guildFlowerHarvestSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_FLOWER_HARVEST, $flowerId, true,false);
            if (!$guildFlowerHarvestSetting) {
                return array(
                    'code' => GameConst::CODE_9998,
                );
            }
            $harvestTime = intval($guildFlowerHarvestSetting['time'] * (100 - $guildFlowerpotSetting['shorten']) / 100);
            if (($reqTime - $updateTime) < $harvestTime) {
                continue;
            }

            $maxStore = $guildFlowerpotSetting['store'];
            $harvestCount = intval(($reqTime - $updateTime) / $harvestTime);
            $surplusTime = ($reqTime - $updateTime) % $harvestTime;

            if ($maxStore < $harvestCount) {
                $harvestCount = $maxStore;
                $surplusTime = 0;
            }

            $flowerList[$flowerId] = $harvestCount;
            if ($harvestCount > 0) {
                $cropSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_PLANT_CROP, $flowerId, true);
                $itemId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_GET, GameConst::ENTITY_TYPE_ITEM, $cropSetting['exp']);
                GameService::get()->handleEntity($userId, $itemId, ($harvestCount*2), true);
                $output .= $itemId.':'.$harvestCount.'|';
                $itemId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_GET, GameConst::ENTITY_TYPE_ITEM, $flowerId);
                GameService::get()->handleEntity($userId, $itemId, $harvestCount, true);
                $output .= $itemId.':'.$harvestCount.'|';
            }

            $guildFlowerpotInfo->setUpdateTime($reqTime - $surplusTime);
            $guildFlowerpotInfoList->updateElement($position,$guildFlowerpotInfo);

        }
        SzPersister::get()->setVoList($guildFlowerpotInfoList);

        if ($output) {
            CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD,$userId,1,$output,'harvest');//获得资源上报
            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_USER_ORBIT, array(
                'clientData' => array(
                    'tid' => 'Module:cluPotb|action:get|',
                    'item' => 'clubPot_get',
                    'value' => 1
                )
            ));

            StatisticsService::get()->saveLog($userId, GameConst::LOG_TYPE_RESOURCE_OUTPUT, array(
                'clientData' => array(
                    'tid' => $output,
                    'item' => 'clubPot_get',
                    'value' => 1
                )
            ));
        }

        return array(
            'code' => GameConst::CODE_1000,
            'harvestList' => $flowerList,
            'guildFlowerpotList' => $guildFlowerpotInfoList->toArray()
        );
    }

    /**
     * 替换花
     * @param $userId
     * @param $position
     * @param $flowerId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function replace($userId,$position,$flowerId,$reqTime) {
        /**
         * @var $guildFlowerpotInfoList GuildFlowerpotInfoVoList
         * @var $guildFlowerpotInfo GuildFlowerpotInfoVo
         */
        $guildFlowerpotInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_FLOWERPOT_INFO);
        $guildFlowerpotInfo = $guildFlowerpotInfoList->getElement($position,false);
        if (!$guildFlowerpotInfo) {
            return array(
                'code' => GameConst::CODE_1019, // 公会花盆未解锁
            );
        }

        $plantFlowerId = $guildFlowerpotInfo->getFlowerId();
        $level = $guildFlowerpotInfo->getLevel();
        $updateTime = $guildFlowerpotInfo->getUpdateTime();

        $guildFlowerpotSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_FLOWERPOT, $level, true,false);
        if (!$guildFlowerpotSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        if ($plantFlowerId) {
            $guildFlowerHarvestSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_FLOWER_HARVEST, $plantFlowerId, true, false);
            if (!$guildFlowerHarvestSetting) {
                return array(
                    'code' => GameConst::CODE_9998,
                );
            }
            $harvestTime = intval($guildFlowerHarvestSetting['time'] * (100 - $guildFlowerpotSetting['shorten']) / 100);
            if (($reqTime - $updateTime) >= $harvestTime) {
                return array(
                    'code' => GameConst::CODE_1020, // 请先收获花
                );
            }
        }

        $guildFlowerpotInfo->setFlowerId($flowerId);
        $guildFlowerpotInfo->setUpdateTime($reqTime);
        $guildFlowerpotInfoList->updateElement($position,$guildFlowerpotInfo);
        SzPersister::get()->setVoList($guildFlowerpotInfoList);

        return array(
            'code' => GameConst::CODE_1000,
            'guildFlowerpotInfo' => $guildFlowerpotInfo->toArray()
        );
    }

    /**
     * 设置公会成员信息
     * @param $userId
     * @param $guildId
     * @param $position
     * @return array
     * @throws SzException
     */
    public function setGuildMemberInfo($userId,$guildId,$position) {
        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            $guildMemberInfo = new GuildMemberInfoVo($guildId, $userId, 0, 0, 0, 0, true);
        }

        $guildMemberInfo->setPosition($position);

        if ($guildMemberInfo->isInsert() && !SzUtility::checkArrayKey($userId, $guildMemberInfoList->getList())) {
            $guildMemberInfoList->addElement($guildMemberInfo);
            $guildMemberInfoList->setElement($userId,$guildMemberInfo);
        } else {
            $guildMemberInfoList->updateElement($userId,$guildMemberInfo);
        }
        SzPersister::get()->setVoList($guildMemberInfoList);

        return $guildMemberInfo->toArray();
    }

    /**
     * 设置用户公会id
     * @param $userId
     * @param $guildId
     * @param $profileInfoList ModuleProfileInfoVoList
     * @throws SzException
     */
    public function setUserGuildId($userId,$guildId,$profileInfoList) {
        /**
         * @var ModuleProfileInfoVo $guildIdProfileInfo
         */
        $guildIdProfileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_GUILD_ID, false);
        if (!$guildIdProfileInfo) {
            $guildIdProfileInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::USER_INFO_TYPE_GUILD_ID,
                0,
                GameService::get()->getReqTime(),
                true
            );
        }
        $guildIdProfileInfo->setInfo($guildId);
        if ($guildIdProfileInfo->isInsert() && !SzUtility::checkArrayKey(GameConst::USER_INFO_TYPE_GUILD_ID, $profileInfoList->getList())) {
            $profileInfoList->addElement($guildIdProfileInfo);
            $profileInfoList->setElement(GameConst::USER_INFO_TYPE_GUILD_ID,$guildIdProfileInfo);
        } else {
            $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_GUILD_ID,$guildIdProfileInfo);
        }
        SzPersister::get()->setVoList($profileInfoList);
    }

    /**
     * 设置公会捐献信息
     * @param $userId
     * @param $profileInfoList ModuleProfileInfoVoList
     * @param null $info
     * @param null $reqTime
     * @throws SzException
     */
    public function setUserDonateInfo($userId,$profileInfoList,$info = null,$reqTime = null) {
        /**
         * @var ModuleProfileInfoVo $guildDonateInfoProfileInfo
         */
        $guildDonateInfoProfileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_GUILD_DONATE_INFO, false);
        if (!$guildDonateInfoProfileInfo) {
            $guildDonateInfoProfileInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::USER_INFO_TYPE_GUILD_DONATE_INFO,
                0,
                GameService::get()->getReqTime(),
                true
            );
        }

        if (!is_null($info)) {
            $guildDonateInfoProfileInfo->setInfo($info);
        }

        if(!is_null($reqTime)) {
            $guildDonateInfoProfileInfo->setTime($reqTime);
        }

        if ($guildDonateInfoProfileInfo->isInsert() && !SzUtility::checkArrayKey(GameConst::USER_INFO_TYPE_GUILD_DONATE_INFO, $profileInfoList->getList())) {
            $profileInfoList->addElement($guildDonateInfoProfileInfo);
            $profileInfoList->setElement(GameConst::USER_INFO_TYPE_GUILD_DONATE_INFO,$guildDonateInfoProfileInfo);
        } else {
            $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_GUILD_DONATE_INFO,$guildDonateInfoProfileInfo);

        }
        SzPersister::get()->setVoList($profileInfoList);
    }

    /**
     * 获取公会捐献信息
     * @param $profileInfoList
     * @return array
     */
    public function getUserDonateInfo($profileInfoList) {
        /**
         * @var ModuleProfileInfoVo $guildDonateInfoProfileInfo
         */
        $guildDonateInfoProfileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_GUILD_DONATE_INFO, false);

        if (!$guildDonateInfoProfileInfo) {
            return array(
                'donateTimes' => 0,
                'donateTime' => 0
            );
        }

        return array(
            'donateTimes' => intval($guildDonateInfoProfileInfo->getInfo()),
            'donateTime' => intval($guildDonateInfoProfileInfo->getTime())
        );
    }

    /**
     * 获取公会id
     * @param $userId
     * @param $profileInfoList
     * @return array|int|null
     */
    public function getUserGuildId($userId, $profileInfoList) {
        $guildId = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_GUILD_ID, $profileInfoList);
        return is_null($guildId ) ? 0 : $guildId ;
    }

    /**
     * 获取成员列表
     * @param $userId
     * @param $position
     * @param $money
     * @return array
     * @throws SzException
     */
    public function getMemberInfo($userId,$position,$money) {
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $donateInfo = $this->getUserDonateInfo($profileInfoList);
        $res = array(
            'pid' => $userId,
            'avatarUrl' => UserService::get()->getHeadIcon($userId, $profileInfoList),
            'name' => UserService::get()->getTownName($userId, $profileInfoList),
            'lv' => UserService::get()->getUserLevel($userId),
            'flowerLv' => UserService::get()->getFlowerLevel1($userId,$profileInfoList),
            'position' => $position,
            'money' => $money,
            'lastLoginTime' => UserService::get()->getUserLastLoginTime($userId),
            'sex' => UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_GENDER, $profileInfoList),
            'donateTime' => $donateInfo['donateTime']
        );

        //获取玩家的头像框数据
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_HEAD_FRAME,false);
        if (!$profileInfo){
            $res['headFrame'] = 0;
        }
        else{
            $res['headFrame'] = $profileInfo->getInfo();
        }

        return $res;
    }

    /**
     * 鲜花分享信息
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function shareInfo($userId,$reqTime) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = $this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $players = array();

        if ($guildMemberInfoList->toArray()) {
            foreach ($guildMemberInfoList->toArray() as $value) {
                if ($userId == $value['userId']) {
                    continue;
                }
                $guildMemberShareInfoList = SzPersister::get()->getVoList($value['userId'], GameConst::ORM_NAME_GUILD_SHARE_INFO);
                if (!count($guildMemberShareInfoList->toArray())) {
                    continue;
                }
                $data = array();
                foreach ($guildMemberShareInfoList->toArray() as $key=>$val) {
                    if (date('Ymd',$val['updateTime']) == date('Ymd',$reqTime) && $val['flowerId']) {
                        $data[$key] = $val;
                    }
                }
                if (!$data) {
                    continue;
                }

                $profileInfoList = SzPersister::get()->getVoList($value['userId'], ModuleProfileConst::ORM_NAME_PROFILE_INFO);
                //获取玩家的头像框数据
                $headFrame = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_HEAD_FRAME,false);
                $players[] = array(
                    'avatarUrl' => UserService::get()->getHeadIcon($value['userId'], $profileInfoList),
                    'name' => UserService::get()->getTownName($value['userId'], $profileInfoList),
                    'pid' => $value['userId'],
                    'flowerLevel' => UserService::get()->getFlowerLevel($value['userId'],$profileInfoList),
                    'flowerLevelExpireTime' => UserService::get()->getFlowerLevelExpireTime($value['userId'],$profileInfoList),
                    'guildShareInfoList' => $data,
                    'headFrame' => !$headFrame ? 0 : $headFrame->getInfo()
                );
            }
        }

        /**
         * @var $guildShareInfoList GuildShareInfoVoList
         * @var $guildShareInfo GuildShareInfoVo
         */
        $guildShareInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_SHARE_INFO);
        $guildShareInfo = $guildShareInfoList->getElement(1,false);
        if (!$guildShareInfo) {
            $guildShareInfo = new GuildShareInfoVo($userId,1,0,0,0,0,true);
            $guildShareInfoList->addElement($guildShareInfo);
            $guildShareInfoList->setElement(1,$guildShareInfo);
            SzPersister::get()->setVoList($guildShareInfoList);
        }

        return array(
            'code' => GameConst::CODE_1000,
            'guildShareInfoList' => $guildShareInfoList->toArray(),
            'guildMemberInfoList' => $players
        );
    }

    /**
     * 鲜花分享
     * @param $userId
     * @param $position
     * @param $flowerId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function share($userId,$position,$flowerId,$reqTime) {
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = $this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005
            );
        }

        /**
         * @var $guildShareInfoList GuildShareInfoVoList
         * @var $guildShareInfo GuildShareInfoVo
         */
        $guildShareInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_SHARE_INFO);
        $guildShareInfo = $guildShareInfoList->getElement($position,false);
        if (!$guildShareInfo) {
            return array(
                'code' => GameConst::CODE_1024
            );
        }

        if ($guildShareInfo->getFlowerId()) {
            return array(
                'code' => GameConst::CODE_1025
            );
        }

        /**
         * @var $seedList PlantSeedVoList
         * @var $seedVo PlantSeedVo
         */
        $seedList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_PLANT_SEED);
        $seedVo = $seedList->getElement($flowerId, false);
        if (!$seedVo) {
            return array(
                'code' => GameConst::CODE_1026
            );
        }
        $count = ($seedVo->getLevel() + 1) * 10;
        $buildEntityId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_CONSUME,GameConst::ENTITY_TYPE_ITEM,$flowerId);
        GameService::get()->handleEntity($userId, $buildEntityId, $count, true);
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD,$userId,0,$buildEntityId.':'.$count,'share');//获得资源上报

        $guildShareInfo->setCount(($count/5));
        $guildShareInfo->setFlowerId($flowerId);
        $guildShareInfo->setUpdateTime($reqTime);
        $guildShareInfoList->updateElement($position,$guildShareInfo);
        SzPersister::get()->setVoList($guildShareInfoList);

        // 更新合约任务进度
        $contractData = ContractService::get()->getContractOpenAndTask($userId,GameConst::CONTRACT_TASK_8,$reqTime);
        if (!empty($contractData) && !empty($contractData['task']) && !empty($contractData['session'])){
            if ($reqTime >= $contractData['start'] && $reqTime <= $contractData['end']){
                ContractService::get()->updateTaskProgress($userId,$contractData['session'],$contractData['task']['pos'],$contractData['task']['taskId'],$reqTime);
            }
        }


        return array(
            'code' => GameConst::CODE_1000,
            'guildShareInfo' => $guildShareInfo->toArray()
        );
    }

    /**
     * 拿取鲜花
     * @param $userId
     * @param $targetId
     * @param $position
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function shareHarvest($userId, $targetId, $position, $reqTime) {
        $redisDb = SzDbFactory::get()->getDb($targetId, SzAbstractDb::DB_TYPE_REDIS);
        if($redisDb->get(GameConst::CACHE_ROB_KEY.$targetId.':'.$position)) {
            return array('code' => GameConst::CODE_1027);
        }

        $redisDb->incr(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);
        $redisDb->expire(GameConst::CACHE_ROB_KEY.$targetId.':'.$position,10);

        if ($position < 1 || $position > 3) {
            $redisDb->delete(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);
            return array('code'=>GameConst::CODE_1030);
        }

        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = $this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            $redisDb->delete(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);
            return array('code' => GameConst::CODE_1005);
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            $redisDb->delete(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);
            return array('code' => GameConst::CODE_9999);
        }

        /**
         * @var $targetProfileInfoList  ModuleProfileInfoVoList
         */
        $targetProfileInfoList = SzPersister::get()->getVoList($targetId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $targetGuildId = $this->getUserGuildId($targetId,$targetProfileInfoList);
        if (!$targetGuildId) {
            $redisDb->delete(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);
            return array('code' => GameConst::CODE_1028);
        }

        if($targetGuildId != $guildId) {
            $redisDb->delete(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);
            return array('code' => GameConst::CODE_1028);
        }

        $addShareNum = $guildInfo->getAddShareNum();

        $userShareNum = $this->getUserGuildShareInfo($profileInfoList,$reqTime);
        if ($userShareNum >= (1 + $addShareNum)) {
            $redisDb->delete(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);
            return array('code' => GameConst::CODE_1029);
        }

        /**
         * @var $guildShareInfoList GuildShareInfoVoList
         * @var $guildShareInfo GuildShareInfoVo
         */
        $guildShareInfoList = SzPersister::get()->getVoList($targetId, GameConst::ORM_NAME_GUILD_SHARE_INFO);
        $guildShareInfo = $guildShareInfoList->getElement($position,false);
        if (!$guildShareInfo) {
            $redisDb->delete(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);
            return array('code' => GameConst::CODE_1024);
        }

        $flowerId = $guildShareInfo->getFlowerId();
        if (!$flowerId || date('Ymd',$reqTime) != date('Ymd',$guildShareInfo->getUpdateTime())) {
            $redisDb->delete(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);
            return array('code' => GameConst::CODE_1039);
        }

        $times = $guildShareInfo->getTimes();

        if ($times >= 5) {
            $redisDb->delete(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);
            return array('code' => GameConst::CODE_1040);
        }

        $buildEntityId = EntityService::get()->buildEntityId(GameConst::ENTITY_ACTION_GET,GameConst::ENTITY_TYPE_ITEM,$flowerId);
        GameService::get()->handleEntity($userId, $buildEntityId, $guildShareInfo->getCount(), true);

        $guildShareInfo->setTimes(++$times);
        $guildShareInfoList->updateElement($position,$guildShareInfo);
        SzPersister::get()->setVoList($guildShareInfoList);

        $message = array(
            'flowerId' => $flowerId,
            'targetId' => $userId,
            'reqTime' => $reqTime,
            'count' => $guildShareInfo->getCount(),
            'npc' => ''
        );
        $redisDb->lTrim(GameConst::CACHE_KEY_GUILD_SHARE_SOCIAL_MESSAGE_LIST.$targetId,0,99);
        $redisDb->lPush(GameConst::CACHE_KEY_GUILD_SHARE_SOCIAL_MESSAGE_LIST.$targetId,json_encode($message,true));
        $redisDb->delete(GameConst::CACHE_ROB_KEY.$targetId.':'.$position);

        $this->setUserGuildShareInfo($userId,$profileInfoList,$reqTime);

        return array(
            'code' =>GameConst::CODE_1000,
            'guildShareInfo' => $guildShareInfo->toArray()
        );
    }

    /**
     * 消息列表
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function shareMessage($userId,$reqTime) {
        $redisDb = SzDbFactory::get()->getDb($userId, SzAbstractDb::DB_TYPE_REDIS);
        $list = $redisDb->lRange(GameConst::CACHE_KEY_GUILD_SHARE_SOCIAL_MESSAGE_LIST.$userId,0,-1);
        $data = array();
        if ($list) {
            foreach ($list as $key => $val) {
                $val = json_decode($val,true);
                if ($val['reqTime'] < ($reqTime - GameConst::SOCIAL_VALID_TIME)) {
                    continue;
                }
                if ($val['targetId']) {
                    $profileInfoList = SzPersister::get()->getVoList($val['targetId'], ModuleProfileConst::ORM_NAME_PROFILE_INFO);
                    $data[$key]['townName'] = UserService::get()->getTownName($userId,$profileInfoList);
                    $data[$key]['npc'] = '';
                } else {
                    $data[$key]['townName'] = '';
                    $data[$key]['npc'] = $val['npc'];
                }
                $orderRewardSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_ORDER_REWARD, $val['flowerId'], true);
                $data[$key]['reqTime'] = $val['reqTime'];
                $data[$key]['flowerId'] = $val['flowerId'];
                $data[$key]['count'] = $val['count'];
                $data[$key]['experience'] = $orderRewardSetting['experience'] * $val['count'];
                $data[$key]['gold'] = $orderRewardSetting['gold'] * $val['count'];
            }
        }

        return array(
            'data' => $data,
            'code' => GameConst::CODE_1000,
        );
    }

    /**
     * 增加分享次数
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function shareAddTimes($userId,$reqTime) {
        $guildShareSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_SHARE, 1, true,false);
        if (!$guildShareSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId =$this->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            return array(
                'code' => GameConst::CODE_1005, // 未加入公会
            );
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         * @var $targetGuildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            return array(
                'code' => GameConst::CODE_9999,
            );
        }

        if($guildMemberInfo->getPosition() > 2) {
            return array(
                'code' => GameConst::CODE_1007,
            );
        }

        $update = true;
        $addShareNum = $guildInfo->getAddShareNum();

        if($addShareNum >= 2) {
            return array(
                'code' => GameConst::CODE_1041,
            );
        }

        if(!$addShareNum) {
            $costNum = $guildShareSetting['add1'];
        } else {
            $costNum = $guildShareSetting['add2'];
        }

        $money = $guildInfo->getMoney();
        if ($money < $costNum) {
            return array(
                'code' => GameConst::CODE_1013,
            );
        }
        $guildInfo->setAddShareNum(++$addShareNum);
        $guildInfo->updateMoney(-$costNum);
        if($update) {
            $guildInfo->setUpdateTime($reqTime);
        }

        SzPersister::get()->setVo($guildInfo);

        //记录
        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $message = array(
            'userId' => $userId,
            'type' => GameConst::GUILD_LOG_TYPE3,
            'sid' => -1,
            'num' => $costNum,
            'pos' => $guildMemberInfo->getPosition(),
            'time' => $reqTime,
            'lv' => -1,
            'share' => $addShareNum,
        );

        $newKey = $reqTime;
        $hasData = $redisDb->hGet(GameConst::CACHE_KEY_GUILD_SKILL_USE_LIST.$guildId,$reqTime);
        if ($hasData){
            $newKey = $reqTime + 1;
        }
        $redisDb->hSet(GameConst::CACHE_KEY_GUILD_SKILL_USE_LIST.$guildId,$newKey,json_encode($message));

        return array(
            'code' => GameConst::CODE_1000,
            'guildInfo' => $guildInfo->toArray()
        );
    }

    /**
     * 开启格子
     * @param $userId
     * @param $position
     * @return array
     * @throws SzException
     */
    public function shareOpenBox($userId,$position) {
        if ($position < 2 || $position > 3) {
            return array('code'=>GameConst::CODE_1030);
        }

        $guildShareSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_SHARE, 1, true,false);
        if (!$guildShareSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }

        /**
         * @var $guildShareInfoList GuildShareInfoVoList
         * @var $guildShareInfo GuildShareInfoVo
         */
        $guildShareInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_SHARE_INFO);
        $guildShareInfo = $guildShareInfoList->getElement($position,false);
        if ($guildShareInfo) {
            return array('code' => GameConst::CODE_1042);
        }

        if ($position == 2) {
            $count = $guildShareSetting['box1'];
        } else {
            $count = $guildShareSetting['box2'];
        }
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_MONEY, $count, true);

        $guildShareInfo = new GuildShareInfoVo($userId,$position,0,0,0,0,true);
        $guildShareInfoList->addElement($guildShareInfo);
        $guildShareInfoList->setElement($position,$guildShareInfo);
        SzPersister::get()->setVoList($guildShareInfoList);

        return array('code' => GameConst::CODE_1000);
    }

    /**
     * 领取 奖励
     * @param $userId
     * @param $position
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function shareReward($userId,$position,$reqTime) {
        $guildShareSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_SHARE, 1, true,false);
        if (!$guildShareSetting) {
            return array(
                'code' => GameConst::CODE_9998,
            );
        }
        /**
         * @var $guildShareInfoList GuildShareInfoVoList
         * @var $guildShareInfo GuildShareInfoVo
         */
        $guildShareInfoList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_SHARE_INFO);
        $guildShareInfo = $guildShareInfoList->getElement($position,false);
        if (!$guildShareInfo) {
            return array('code' => GameConst::CODE_1024);
        }
        $flowerId = $guildShareInfo->getFlowerId();
        if (!$flowerId) {
            return array('code' => GameConst::CODE_1026);
        }

        $updateTime = $guildShareInfo->getUpdateTime();
        if (date('Ymd',$reqTime) == date('Ymd',$updateTime)) {
            return array('code' => GameConst::CODE_1043);
        }

        $count = $guildShareInfo->getCount();

        if ($guildShareInfo->getTimes() >= 5) {
            GameService::get()->handleEntity($userId, GameConst::GUILD_SHARE_ITEM_ID, $guildShareSetting['extraRewardNum'], true);
        } else {
            $redisDb = SzDbFactory::get()->getDb($userId, SzAbstractDb::DB_TYPE_REDIS);
            for ($i=1;$i<=(5 - $guildShareInfo->getTimes());$i++) {
                $key = array_rand($guildShareSetting['npc']);
                $npc = $guildShareSetting['npc'][$key];
                $message = array(
                    'flowerId' => $flowerId,
                    'targetId' => 0,
                    'reqTime' => $reqTime,
                    'count' => $count,
                    'npc' => $npc
                );
                $redisDb->lTrim(GameConst::CACHE_KEY_GUILD_SHARE_SOCIAL_MESSAGE_LIST.$userId,0,99);
                $redisDb->lPush(GameConst::CACHE_KEY_GUILD_SHARE_SOCIAL_MESSAGE_LIST.$userId,json_encode($message,true));
            }
        }

        $orderRewardSetting = SzConfig::get()->loadAppConfig(GameConst::CONFIG_ORDER_REWARD, $flowerId, true);
        $experience = $orderRewardSetting['experience'] * $count * 5;
        $gold = $orderRewardSetting['gold'] * $count * 5;

        GameService::get()->handleEntity($userId, GameConst::GUILD_SHARE_ITEM_ID, $guildShareSetting['rewardNum'], true);
        UserService::get()->updateExp($userId, $experience,null, false);
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_GET_TOKEN, $gold, true);

        $guildShareInfo->setTimes(0);
        $guildShareInfo->setCount(0);
        $guildShareInfo->setFlowerId(0);
        $guildShareInfoList->updateElement($position,$guildShareInfo);
        SzPersister::get()->setVoList($guildShareInfoList);

        return array(
            'code' => GameConst::CODE_1000,
            'experience' => $experience,
            'gold' => $gold
        );
    }

    /**
     * 收藏鲜花
     * @param $userId
     * @param $flowerIds
     * @return array
     */
    public function shareCollection($userId,$flowerIds) {
        if (count($flowerIds) > 20) {
            return array(
                'code' => GameConst::CODE_1030
            );
        }
        $redisDb = SzDbFactory::get()->getDb($userId, SzAbstractDb::DB_TYPE_REDIS);
        $flowerIds = json_encode($flowerIds,true);
        $redisDb->set(GameConst::CACHE_GUILD_SHARE_COLLECTION_CACHE_KEY.$userId,$flowerIds);
        return array(
            'code' => GameConst::CODE_1000
        );
    }

    /**
     * 设置公会分享鲜花信息
     * @param $userId
     * @param $profileInfoList ModuleProfileInfoVoList
     * @param $reqTime
     * @throws SzException
     */
    public function setUserGuildShareInfo($userId,$profileInfoList,$reqTime) {
        /**
         * @var ModuleProfileInfoVo $guildShareInfoProfileInfo
         */
        $guildShareInfoProfileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_GUILD_SHARE_INFO, false);
        if (!$guildShareInfoProfileInfo) {
            $guildShareInfoProfileInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::USER_INFO_TYPE_GUILD_SHARE_INFO,
                0,
                0,
                true
            );
        }

        if (date('Ymd',$reqTime) == date('Ymd',$guildShareInfoProfileInfo->getTime())) {
            $info = $guildShareInfoProfileInfo->getRawInfo() + 1;
            $guildShareInfoProfileInfo->setInfo(intval($info));
        } else {
            $guildShareInfoProfileInfo->setInfo(1);
            $guildShareInfoProfileInfo->setTime($reqTime);
        }

        if ($guildShareInfoProfileInfo->isInsert() && !SzUtility::checkArrayKey(GameConst::USER_INFO_TYPE_GUILD_SHARE_INFO, $profileInfoList->getList())) {
            $profileInfoList->addElement($guildShareInfoProfileInfo);
            $profileInfoList->setElement(GameConst::USER_INFO_TYPE_GUILD_SHARE_INFO,$guildShareInfoProfileInfo);
        } else {
            $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_GUILD_SHARE_INFO,$guildShareInfoProfileInfo);

        }
        SzPersister::get()->setVoList($profileInfoList);
    }

    /**
     * @param $profileInfoList ModuleProfileInfoVoList
     * @param $reqTime
     * @return int
     * @throws SzException
     */
    public function getUserGuildShareInfo($profileInfoList,$reqTime) {
        /**
         * @var ModuleProfileInfoVo $guildShareInfoProfileInfo
         */
        $guildShareInfoProfileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_GUILD_SHARE_INFO, false);

        if (!$guildShareInfoProfileInfo) {
            return 0;
        }

        if (date('Ymd',$reqTime) == date('Ymd',$guildShareInfoProfileInfo->getTime())) {
            return intval($guildShareInfoProfileInfo->getInfo());
        } else {
            return 0;
        }
    }

   // 20200628
    /**
     * 刷新工会店铺
     * @param $userId
     * @param $type
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function refreshGuildShop($userId,$type,$reqTime) {
        $res = array(
            'code' => 0,
            'times' => 0,
            'freeCount' => 0
        );

        $systemTime = GameService::get()->getSysTime();
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         * @var $yesTodayTimeInfo  ModuleProfileInfoVo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $yesTodayTimeInfo = $profileInfoList->getElement(GameConst::GUILD_SHOP_SYS_REFRESH_TIME,false);
        if ($yesTodayTimeInfo){
            if ($type == 0){
                if (date("Ymd",$systemTime) == date("Ymd",json_decode($yesTodayTimeInfo->getInfo()))){
                    if (date("H",$reqTime) < 12){
                        $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['TIME_ERR_CANT_REFRESH'];//时间错误，不可刷新
                        return $res;
                    }
                }
                elseif (date("Ymd",$systemTime) > date("Ymd",json_decode($yesTodayTimeInfo->getInfo()))){
                    if (date("H",$reqTime) < 12){
                        $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['TIME_ERR_CANT_REFRESH'];//时间错误，不可刷新
                        return $res;
                    }
                }
                else{
                    $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['TIME_ERR_CANT_REFRESH'];//时间错误，不可刷新
                    return $res;
                }
            }
        }
        else{
            $yesTodayTimeInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::GUILD_SHOP_SYS_REFRESH_TIME,
                $reqTime,
                $reqTime,
                true
            );
        }

        //获取配置文件
        $freeNum = 4;
        //获取配置文件
        $itemConfs = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_STORE, null, true);
        $skillConfs = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_SKILL, null, true);
        $refreshPriceConf = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'guildStoreRefreshPrice', true)['value'];
        if (!$freeNum || !$refreshPriceConf || !$skillConfs || !$itemConfs){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家工会
        $guildId = GuildService::get()->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        $isUpdate = false;
        $leftRefreshTimes = 0;
        //工会商店刷新数据
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         * @var ModuleProfileInfoVo $guildShopRefreshTimes
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        //获取玩家的每日刷新
        $guildShopRefreshTimes = $profileInfoList->getElement(GameConst::GUILD_SHOP_REFRESH_TIME_MSG,false);
        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        //获取玩家技能类型2
        $temp = array();
        foreach ($skillConfs as $key=>$skill){
            if ($skill['skillType'] == GameConst::GUILD_SKILL_TYPE_2){
                $temp[] = $skill;
            }
        }
        /**
         * @var $guildSkillList  GuildSkillVoList
         * @var $guildSkill  GuildSkillVo
         */
        $highTime = 0;
        $guildSkillList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_SKILL);
        foreach ($temp as $k=>$conf){
            $guildSkill = $guildSkillList->getElement($conf['skillId'],false);
            if ($guildSkill){
                $highTime = $conf['skill'][0];
            }
        }

        //获取最高次数
        if (!empty($temp)){
            if (!$guildShopRefreshTimes){
                $refreshData = $highTime.':'.$reqTime;
                $guildShopRefreshTimes = new ModuleProfileInfoVo(
                    $userId,
                    GameConst::GUILD_SHOP_REFRESH_TIME_MSG,
                    json_encode($refreshData),
                    $reqTime,
                    true
                );
                $leftRefreshTimes = $highTime;
            }
            else{
                $leftData = explode(':',json_decode($guildShopRefreshTimes->getInfo()));
                $leftRefreshTimes = $leftData[0] - 1;
                $lastTime = $leftData[1];
                if (date('Ymd',$lastTime) != date('Ymd',$reqTime)){//隔天了
                    $isUpdate = true;
                    $leftRefreshTimes = $highTime;
                }
            }
        }
        else{
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //不同类型进行不同处理
        if ($type == 1){//有技能工会免费刷
            if ($leftRefreshTimes < 0){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['LEFT_REFRESH_TIME_ZERO'];//刷新次数为0
                return $res;
            }
            if ($isUpdate){
                $guildShopRefreshTimes->setInfo($leftRefreshTimes.':'.$reqTime);
            }
        }
        if ($type == 2){//消耗资源刷
            //消耗玩家资源
//            $leftNum = $guildMemberInfo->getMoney() - $refreshPriceConf;
//            if ($leftNum < 0){
//                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RESOURCE_NOT_ENOUGH'];//资源不够
//                return $res;
//            }
//            $guildMemberInfo->setMoney($leftNum);
//            $guildMemberInfoList->updateElement($userId,$guildMemberInfo);
//            SzPersister::get()->setVoList($guildMemberInfoList);
            GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_GUILD_MONEY, $refreshPriceConf, true, true);
            CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD_SHOP,$userId,0,'GUILD_SHARE_ITEM_ID:-'.$refreshPriceConf,'refreshGuildShop2');//获得资源上报
        }
        if ($type > 2){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['LEFT_REFRESH_TIME_ZERO'];//刷新次数为0
            return $res;
        }

        $result = $this->refreshShop($userId,$guildId,$itemConfs,$skillConfs,$freeNum,$reqTime,$type);
        if ($result != 0){
            $res['code'] = $result;
            return $res;
        }

        if ($guildShopRefreshTimes->isInsert() && !SzUtility::checkArrayKey(GameConst::GUILD_SHOP_REFRESH_TIME_MSG, $profileInfoList->getList())) {
            $profileInfoList->addElement($guildShopRefreshTimes);
            $profileInfoList->setElement(GameConst::GUILD_SHOP_REFRESH_TIME_MSG,$guildShopRefreshTimes);
        } else {
            $guildShopRefreshTimes->setInfo($leftRefreshTimes.':'.$reqTime);
            $profileInfoList->updateElement(GameConst::GUILD_SHOP_REFRESH_TIME_MSG, $guildShopRefreshTimes);
        }

        if ($yesTodayTimeInfo->isInsert() && !SzUtility::checkArrayKey(GameConst::GUILD_SHOP_SYS_REFRESH_TIME, $profileInfoList->getList())) {
            $profileInfoList->addElement($yesTodayTimeInfo);
            $profileInfoList->setElement(GameConst::GUILD_SHOP_SYS_REFRESH_TIME,$yesTodayTimeInfo);
        } else {
            $yesTodayTimeInfo->setInfo($reqTime);
            $profileInfoList->updateElement(GameConst::GUILD_SHOP_SYS_REFRESH_TIME, $yesTodayTimeInfo);
        }

        SzPersister::get()->setVoList($profileInfoList);

        /**
         * 获取玩家商品数据
         * @var ModuleItemVoList $itemList
         */
        $itemList = SzPersister::get()->getVoList($userId, ModuleItemConst::ORM_NAME_ITEM);
        $leftNum = $itemList->getItemDefCount(GameConst::GUILD_SHARE_ITEM);

        $res['times'] = $leftRefreshTimes;
        $res['freeCount'] = $leftNum;
        return $res;
    }

    /**
     * @param $userId
     * @param $guildId
     * @param $itemConfs
     * @param $skillConfs
     * @param $freeNum
     * @param $reqTime
     * @param $type
     * @return int|mixed
     * @throws SzException
     */
    public function refreshShop($userId,$guildId,$itemConfs,$skillConfs,$freeNum,$reqTime,$type){
        $code = 0;
        //获取玩家工会技能数据
        /**
         * @var $guildSkillList  GuildSkillVoList
         * @var $guildSkill  GuildSkillVo
         */
        $maybeWeight = 0;
        $guildSkills = array();
        $skillWeight = array();
        $guildSkillList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_SKILL);
        if ($guildSkillList->getListCount() != 0){
            $guildSkills = $guildSkillList->toArray();
            $maybeWeight = 0;
            foreach ($guildSkills as $key=>$data){
                $skillConf = $skillConfs[$data['skillId']];
                if ($skillConf['skillType'] == GameConst::GUILD_SKILL_TYPE_1){
                    $maybeWeight += $skillConf['skill'][0];
                    $skillWeight[] = array('sid' => $data['skillId'],'weight' => $skillConf['skill'][1]);
                }
            }
        }
        $skillWeight[] = array('sid' => 0,'weight' => 100 - $maybeWeight);

        //获取玩家商店数据
        /**
         * @var $guildShopList  GuildShopVoList
         * @var $guildShop  GuildShopVo
         */
        $guildShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_SHOP);
        if ($guildShopList->getListCount() == 0){
            for ($i = 1;$i <= $freeNum;$i++){
                $sale = 0;
                //折扣计算
                $sid = $this->randomSidByWeight($skillWeight)['sid'];
                if ($sid != 0){
                    $sale = $skillConfs[$sid]['skill'][1];
                }

                //随机商品
                $itemConfId = array_rand($itemConfs);
                $guildShop = new GuildShopVo(
                    $userId,
                    $i,
                    $itemConfId,
                    $itemConfs[$itemConfId]['itemNum'],
                    $sale,
                    3,
                    $reqTime,
                    $reqTime,
                    true
                );
                $guildShopList->addElement($guildShop);
                $guildShopList->setElement($i,$guildShop);
            }
        }
        else{
            foreach ($guildShopList->toArray() as $pos=>$value){
                $guildShop = $guildShopList->getElement($value['pos'],false);
                if ($guildShop){
                    $sale = 0;
                    //折扣计算
                    $sid = $this->randomSidByWeight($skillWeight)['sid'];
                    if ($sid != 0){
                        $sale = $skillConfs[$sid]['skill'][1];
                    }
                    $itemConfId = array_rand($itemConfs);
                    if ($guildShop->getState() != 3){
                        $guildShop->setState(3);
                    }

                    $guildShop->setUpdateTime($reqTime);
                    $guildShop->setNum($itemConfs[$itemConfId]['itemNum']);
                    $guildShop->setItem($itemConfId);
                    $guildShop->setSale($sale);
                    $guildShopList->updateElement($value['pos'], $guildShop);
                }
            }
        }

        SzPersister::get()->setVoList($guildShopList);
        return $code;
    }

    /**
     * @param $weighArr
     * @return mixed
     */
    public function randomSidByWeight($weighArr){
        $weight = 0;
        $temp = array();
        foreach ($weighArr as $data){
            $weight += $data['weight'];
            for ($i=0;$i<$data['weight'];$i++){
                $temp[] = $data;
            }
        }
        $use = rand(0, $weight-1);
        return $temp[$use];
    }



    /**
     * 获取玩家工会商店数据
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getGuildShopInfo($userId,$reqTime) {
        $res = array(
            'code' => 0,
            'info' => array(),
            'time' => 0
        );

        //获取玩家工会
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = GuildService::get()->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        //获取玩家商店数据
        /**
         * @var $guildShopList  GuildShopVoList
         * @var $guildShop  GuildShopVo
         */
        $guildShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_SHOP);
        if ($guildShopList->getListCount() == 0){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $free = array(false,false,false,false);
        $itemConfs =  SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_STORE, null, true);
        foreach ($guildShopList->toArray() as $key=>$data){
            $res['info'][] = $data;
            if ($key <= 4 && $data['pos'] <= 4){
                $free[$key - 1] = true;
            }
        }

        foreach ($free as $key=>$state){
            if (!$state){
                //随机商品
                $itemConfId = array_rand($itemConfs);
                $guildShop = new GuildShopVo(
                    $userId,
                    $key + 1,
                    $itemConfId,
                    $itemConfs[$itemConfId]['itemNum'],
                    0,
                    3,
                    $reqTime,
                    $reqTime,
                    true
                );
                $guildShopList->addElement($guildShop);
                $guildShopList->setElement($key + 1,$guildShop);
                $res['info'][] = $guildShop->toArray();
            }
        }
        SzPersister::get()->setVoList($guildShopList);

        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         * @var $yesTodayTimeInfo  ModuleProfileInfoVo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildShopRefreshTimes = $profileInfoList->getElement(GameConst::GUILD_SHOP_REFRESH_TIME_MSG,false);
        $leftData = array();
        if ($guildShopRefreshTimes){
            $leftData = explode(':',json_decode($guildShopRefreshTimes->getInfo()));
        }

        $res['time'] = empty($leftData) ? 0 : $leftData[0];
        return $res;
    }

    /**
     * 解锁工会商店栏位
     * @param $userId
     * @param $pos
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function unlockGuildShopPosition($userId,$pos,$reqTime) {
        $res = array(
            'code' => 0,
            'info' => array(),
            'freeCount' => 0
        );

        //获取配置文件
        $freeNum = 4;
        //获取配置文件
        $posPriceConf = SzConfig::get()->loadAppConfig(ModuleProfileConst::CONFIG_PROFILE, 'guildStoreUnlock', true)['value'];
        $itemConfs = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_STORE, null, true);
        $skillConfs = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_SKILL, null, true);
        if (!$posPriceConf || !$freeNum || !$itemConfs || !$skillConfs){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }
        if ($pos < $freeNum){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CELL_NULOCK'];//栏位已经解锁
            return $res;
        }

        $needConf = array();
        $tempConf = explode(';',$posPriceConf);
        foreach ($tempConf as $key=>$conf){
            $needConf = explode(':',$conf);
            if ($needConf[0]*$freeNum >= $pos){
                $needConf = explode(':',$tempConf[$key]);
                break;
            }
        }
        if (empty($needConf)){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家工会
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = GuildService::get()->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        //获取玩家当前位置的商品数据
        /**
         * @var $guildShopList  GuildShopVoList
         * @var $guildShop  GuildShopVo
         */
        $guildShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_SHOP);
        $guildShop = $guildShopList->getElement($pos,false);
        if ($guildShop){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CELL_NULOCK'];//栏位已经解锁
            return $res;
        }

        //消耗玩家资源
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_GUILD_MONEY, $needConf[1], true, true);
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD_SHOP,$userId,0,GameConst::ENTITY_ACTION_CONSUME_MONEY.':'.$needConf[1],'unlockGuildShop');//消耗资源上报

        /**
         * @var $guildSkillList  GuildSkillVoList
         * @var $guildSkill  GuildSkillVo
         */
        $maybeWeight = 0;
        $guildSkills = array();
        $skillWeight = array();
        $guildSkillList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_SKILL);
        if ($guildSkillList->getListCount() != 0){
            $guildSkills = $guildSkillList->toArray();
            $maybeWeight = 0;
            foreach ($guildSkills as $key=>$data){
                $skillConf = $skillConfs[$data['skillId']];
                if ($skillConf['skillType'] == GameConst::GUILD_SKILL_TYPE_1){
                    $maybeWeight += $skillConf['skill'][0];
                    $skillWeight[] = array('sid' => $data['skillId'],'weight' => $skillConf['skill'][1]);
                }
            }
        }

        $skillWeight[] = array('sid' => 0,'weight' => 100 - $maybeWeight);
        $sale = 0;
        //折扣计算
        $sid = $this->randomSidByWeight($skillWeight)['sid'];
        if ($sid != 0){
            $sale = $skillConfs[$sid]['skill'][1];
        }

        $itemId = array_rand($itemConfs);
        //解锁
        $guildShop = new GuildShopVo(
            $userId,
            $pos,
            $itemId,
            $itemConfs[$itemId]['itemNum'],
            $sale,
            3,
            $reqTime,
            $reqTime,
            true
        );
        $guildShopList->addElement($guildShop);
        $guildShopList->setElement($pos,$guildShop);
        SzPersister::get()->setVoList($guildShopList);

        /**
         * 获取玩家商品数据
         * @var ModuleItemVoList $itemList
         */
        $itemList = SzPersister::get()->getVoList($userId, ModuleItemConst::ORM_NAME_ITEM);
        $leftNum = $itemList->getItemDefCount(GameConst::GUILD_SHARE_ITEM);

        $res['info'] = $guildShop->toArray();
        $res['freeCount'] = $leftNum;
        return $res;
    }

    /**
     * 反过来，真傻
     * @param $userId
     * @param $pos
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function posTurnOff($userId,$pos,$reqTime) {
        $res = array(
            'code' => 0,
            'state' => false
        );

        //获取玩家工会
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = GuildService::get()->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        //获取玩家当前位置的商品数据
        /**
         * @var $guildShopList  GuildShopVoList
         * @var $guildShop  GuildShopVo
         */
        $guildShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_SHOP);
        $guildShop = $guildShopList->getElement($pos,false);
        if (!$guildShop){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];
            return $res;
        }

        $state = $guildShop->getState();
        if ($state == 0){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GUILD_SHOP_CELL_NOT_NULOCK'];//栏位未解锁
            return $res;
        }
        if ($state == 1){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['HAS_TURNED_OFF'];//已经翻开
            return $res;
        }
        if ($state == 2){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GUILD_SHOP_HAS_SALED'];//商品已经售出
            return $res;
        }

        //翻
        $guildShop->setState(1);
        $guildShop->setUpdateTime($reqTime);
        $guildShopList->updateElement($pos,$guildShop);
        SzPersister::get()->setVoList($guildShopList);

        $res['state'] = true;
        return $res;
    }

    /**
     * 购买工会商店
     * @param $userId
     * @param $pos
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function buyGuildItem($userId,$pos,$reqTime){
        $res = array(
            'code' => 0,
            'state' => false,
            'use' => 0,
            'freeCount' => 0
        );

        //获取玩家工会
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = GuildService::get()->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        //获取玩家当前位置的商品数据
        /**
         * @var $guildShopList  GuildShopVoList
         * @var $guildShop  GuildShopVo
         */
        $guildShopList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_GUILD_SHOP);
        $guildShop = $guildShopList->getElement($pos,false);
        if (!$guildShop){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['DATA_NOT_EXIST'];//数据不存在
            return $res;
        }

        $state = $guildShop->getState();
        if ($state == 0){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GUILD_SHOP_CELL_NOT_NULOCK'];//栏位未解锁
            return $res;
        }
        if ($state == 2){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['GUILD_SHOP_HAS_SALED'];//商品已经售出
            return $res;
        }
        if ($state == 3){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['TURN_OFF'];//请先翻开
            return $res;
        }

        //获取配置文件
        $guildShopConf = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_STORE, $guildShop->getItem(), true);
        if (!$guildShopConf){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //消耗玩家资源
//        $leftNum = $guildMemberInfo->getMoney() - ceil($guildShopConf['price']*(1 - $guildShop->getSale()/100));
//        if ($leftNum < 0){
//            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RESOURCE_NOT_ENOUGH'];//资源不够
//            return $res;
//        }
//        $guildMemberInfo->setMoney($leftNum);
//        $guildMemberInfoList->updateElement($userId,$guildMemberInfo);
//        SzPersister::get()->setVoList($guildMemberInfoList);
        $needNum = $guildShopConf['price'];
        if ($guildShop->getSale()){
            $needNum = ceil($guildShopConf['price']*($guildShop->getSale()/10));
        }
        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_CONSUME_GUILD_MONEY, $needNum, true, true);
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD_SHOP,$userId,0,'GUILD_SHARE_ITEM_ID:-'.$needNum,'buyGuildItem');//获得资源上报

        $guildShop->setState(2);
        $guildShop->setUpdateTime($reqTime);
        $guildShopList->updateElement($pos,$guildShop);
        SzPersister::get()->setVoList($guildShopList);

        //获得道具
        $itemNum = $guildShop->getNum();
        GameService::get()->handleEntity($userId, $guildShopConf['itemId'], $itemNum, true, true);
        CommonFunctionService::get()->reportResourceUseDetails(GameConst::FUNC_GUILD_SHOP,$userId,1,$guildShopConf['itemId'].':'.$itemNum,'buyGuildItem');//获得资源上报

        /**
         * 获取玩家商品数据
         * @var ModuleItemVoList $itemList
         */
        $itemList = SzPersister::get()->getVoList($userId, ModuleItemConst::ORM_NAME_ITEM);
        $leftNum = $itemList->getItemDefCount(GameConst::GUILD_SHARE_ITEM);

        $res['state'] = true;
        $res['use'] = $needNum;
        $res['freeCount'] = $leftNum;
        return $res;
    }

    /**
     * 获取技能面板
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getGuildSkillInfo($userId,$reqTime) {
        $res = array(
            'code' => 0,
            'info' => array()
        );

        //获取玩家工会
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = GuildService::get()->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        //获取玩家工会技能
        /**
         * @var $guildSkillList  GuildSkillVoList
         * @var $guildSkill  GuildSkillVoVo
         */
        $guildSkillList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_SKILL);
        if ($guildSkillList->getListCount() != 0){
            foreach ($guildSkillList->toArray() as $key=>$data){
                $res['info'][] = $data;
            }
        }

        return $res;
    }

    /**
     * 解锁工会技能
     * @param $userId
     * @param $sid
     * @param $page
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function unlockGuildSkill($userId,$sid,$page,$reqTime) {
        $res = array(
            'code' => 0,
            'info' => array()
        );

        //获取配置文件
        $useConfs = array();
        $posConf = 2;
        $skillConfs = SzConfig::get()->loadAppConfig(GameConst::CONFIG_GUILD_SKILL, null, true);
        foreach ($skillConfs as $key=>$conf){
            if ($conf['page'] == $page){
                $useConfs[$conf['skillId']] = $conf;
            }
        }
        if (empty($useConfs) || !$posConf){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['CONFIG_NOT_EXIST'];//配置不存在
            return $res;
        }

        //获取玩家工会
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = GuildService::get()->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        /**
         * @var GuildInfoVo $guildInfo
         */
        $guildInfo = SzPersister::get()->getVo(null, GameConst::ORM_NAME_GUILD_INFO, $guildId);
        if (!$guildInfo) {
            return array(
                'code' => GameConst::CODE_9999 // 公会不存在
            );
        }

        //获取玩家当前职位
        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        $guildMemberInfoList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_MEMBER_INFO);
        $guildMemberInfo = $guildMemberInfoList->getElement($userId,false);
        if (!$guildMemberInfo) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        $position = $guildMemberInfo->getPosition();
        if ($position > $posConf){
            $res['code'] = GameConst::CODE_1007;
            return $res;
        }

        //获取玩家当前公会的技能解锁情况
        /**
         * @var $guildSkillList  GuildSkillVoList
         * @var $preSkillData  GuildSkillVo
         * @var $curSkillData  GuildSkillVo
         */
        $guildSkillList = SzPersister::get()->getVoList($guildId, GameConst::ORM_NAME_GUILD_SKILL);
        if ($useConfs[$sid]['prepose'] != 0){
            $preSkillData = $guildSkillList->getElement($useConfs[$sid]['prepose'],false);
            if (!$preSkillData){
                $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['PRE_SKILL_NOT_UNLOCK'];//前置未解锁
                return $res;
            }
        }

        $curSkillData = $guildSkillList->getElement($useConfs[$sid]['skillId'],false);
        if ($curSkillData){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['SKILL_UNLOCK'];//技能已经解锁
            return $res;
        }

        //消耗工会资源
        $leftGuildResource = $guildInfo->getMoney() - $useConfs[$sid]['price'];
        if ($leftGuildResource < 0){
            $res['code'] = GameConst::EXCEPTION_CODE_ARRAY['RESOURCE_NOT_ENOUGH'];//资源不足
            return $res;
        }
        $guildInfo->setMoney($leftGuildResource);
        SzPersister::get()->setVo($guildInfo);

        //解锁工会技能
        $curSkillData = new GuildSkillVo(
            $guildId,
            $sid,
            $reqTime,
            $reqTime,
            $useConfs[$sid]['time'] ? $useConfs[$sid]['time'] : -1,
            true
        );
        $guildSkillList->addElement($curSkillData);
        $guildSkillList->setElement($sid,$curSkillData);
        SzPersister::get()->setVoList($guildSkillList);

        //记录
        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $message = array(
            'userId' => $userId,
            'type' => GameConst::GUILD_LOG_TYPE1,
            'sid' => $sid,
            'num' => $useConfs[$sid]['price'],
            'pos' => $position,
            'time' => $reqTime,
            'lv' => -1,
            'share' => -1,
        );

        $newKey = $reqTime;
        $hasData = $redisDb->hGet(GameConst::CACHE_KEY_GUILD_SKILL_USE_LIST.$guildId,$reqTime);
        if ($hasData){
            $newKey = $reqTime + 1;
        }
        $redisDb->hSet(GameConst::CACHE_KEY_GUILD_SKILL_USE_LIST.$guildId,$newKey,json_encode($message));

        //技能具体效果
        if ($useConfs[$sid]['skillType'] == GameConst::GUILD_SKILL_TYPE_2){
            //给所有成员加次数
            $this->addRefreshTime($guildMemberInfoList,$useConfs,$sid,$reqTime);
        }

        $res['info'] = $curSkillData->toArray();
        return $res;
    }

    /**
     * 给所有成员加次数
     * @param $guildMemberInfoList
     * @param $useConfs
     * @param $sid
     * @param $reqTime
     * @throws SzException
     */
    public function addRefreshTime($guildMemberInfoList,$useConfs,$sid,$reqTime) {
        /**
         * @var $guildMemberInfoList GuildMemberInfoVoList
         * @var $guildMemberInfo GuildMemberInfoVo
         */
        foreach ($guildMemberInfoList->toArray() as $key=>$data){
            /**
             * @var $profileInfoList  ModuleProfileInfoVoList
             * @var ModuleProfileInfoVo $guildShopRefreshTimes
             */
            $profileInfoList = SzPersister::get()->getVoList($data['userId'], ModuleProfileConst::ORM_NAME_PROFILE_INFO);
            $guildShopRefreshTimes = $profileInfoList->getElement(GameConst::GUILD_SHOP_REFRESH_TIME_MSG,false);
            if (!$guildShopRefreshTimes){
                $guildShopRefreshTimes = new ModuleProfileInfoVo(
                    $data['userId'],
                    GameConst::GUILD_SHOP_REFRESH_TIME_MSG,
                    json_encode('1:'.$reqTime),//策划说的写死...
                    $reqTime,
                    true
                );
            }

            if ($guildShopRefreshTimes->isInsert() && !SzUtility::checkArrayKey(GameConst::GUILD_SHOP_REFRESH_TIME_MSG, $profileInfoList->getList())) {
                $profileInfoList->addElement($guildShopRefreshTimes);
                $profileInfoList->setElement(GameConst::GUILD_SHOP_REFRESH_TIME_MSG,$guildShopRefreshTimes);
            } else {
                //策划说的写死
                $leftData = explode(':',json_decode($guildShopRefreshTimes->getInfo()));
                if ($leftData[0] > 0){
                    $guildShopRefreshTimes->setInfo(($leftData[0] + 1).':'.$reqTime);
                }
                else{
                    $guildShopRefreshTimes->setInfo('1:'.$reqTime);
                }
                $profileInfoList->updateElement(GameConst::GUILD_SHOP_REFRESH_TIME_MSG, $guildShopRefreshTimes);
            }
            SzPersister::get()->setVoList($profileInfoList);
        }
    }

    /**
     * 使用技能
     * @param $userId
     * @param $sid
     * @param $reqTime
     * @return array
     */
    public function useGuildSkill($userId,$sid,$reqTime) {
        $res = array();

        return $res;
    }

    /**
     * 获取工会技能使用明细
     * @param $userId
     * @param $reqTime
     * @return array
     * @throws SzException
     */
    public function getGuildSkillUseInfo($userId,$reqTime) {
        $res = array(
            'code' => 0,
            'info' => array()
        );

        //获取玩家工会
        /**
         * @var $profileInfoList  ModuleProfileInfoVoList
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $guildId = GuildService::get()->getUserGuildId($userId,$profileInfoList);
        if (!$guildId) {
            $res['code'] = GameConst::CODE_9999;
            return $res;
        }

        $redisDb = SzDbFactory::get()->getDb(null, SzAbstractDb::DB_TYPE_REDIS);
        $hash = $redisDb->hGetAll(GameConst::CACHE_KEY_GUILD_SKILL_USE_LIST.$guildId);
        if ($hash) {
            foreach ($hash as $key => $val) {
                if ($key < $reqTime - 86400*3){
                    $redisDb->hDel(GameConst::CACHE_KEY_GUILD_SKILL_USE_LIST.$guildId,$key);
                    continue;
                }
//                $data = json_decode($val,true);
//                $res['info'][$data['userId']][] = $data;
//                $res['info'][$data['userId']]['buyerData'] = ActivityService::get()->getUserInfo($data['userId'],0);
                $res['info'][] = json_decode($val,true);
            }
        }

        return $res;
    }



}